setfenv(1, require'winapi')
require'winapi.color'
require'winapi.cursor'

ffi.cdef[[
typedef struct tagWNDCLASSEXW {
	 UINT        cbSize;
	 UINT        style;
	 WNDPROC     lpfnWndProc;
	 int         cbClsExtra;
	 int         cbWndExtra;
	 HINSTANCE   hInstance;
	 HICON       hIcon;
	 HCURSOR     hCursor;
	 HBRUSH      hbrBackground;
	 LPCWSTR     lpszMenuName;
	 LPCWSTR     lpszClassName;
	 HICON       hIconSm;
} WNDCLASSEXW, *PWNDCLASSEXW,  *NPWNDCLASSEXW,  *LPWNDCLASSEXW;

ATOM RegisterClassExW(const WNDCLASSEXW *);

BOOL UnregisterClassW(
     LPCWSTR lpClassName,
     HINSTANCE hInstance);

]]

CS_VREDRAW = 0x0001
CS_HREDRAW = 0x0002
CS_DBLCLKS = 0x0008
CS_OWNDC = 0x0020
CS_CLASSDC = 0x0040
CS_PARENTDC = 0x0080
CS_NOCLOSE = 0x0200
CS_SAVEBITS = 0x0800
CS_BYTEALIGNCLIENT = 0x1000
CS_BYTEALIGNWINDOW = 0x2000
CS_GLOBALCLASS = 0x4000
CS_IME = 0x00010000
CS_DROPSHADOW = 0x00020000

WNDCLASSEXW = struct{
	ctype = 'WNDCLASSEXW', size = 'cbSize',
	fields = sfields{
		'name', 'lpszClassName', wcs,
		'style', 'style', flags,
		'proc', 'lpfnWndProc', pass,
		'icon', 'hIcon', 'HICON',
		'small_icon', 'hIconSm', 'HICON',
		'cursor', 'hCursor', 'HCURSOR',
		'background', 'hbrBackground', 'HBRUSH',
	},
	defaults = {
		style = bit.bor(CS_HREDRAW, CS_VREDRAW),
		background = COLOR_WINDOW,
		cursor = LoadCursor(IDC_ARROW),
	}
}

function RegisterClass(info)
	return checknz(ffi.C.RegisterClassExW(WNDCLASSEXW(info)))
end

function UnregisterClass(class)
	return checknz(ffi.C.UnregisterClassW(ffi.cast('LPCWSTR', wcs(class)), nil))
end

if not ... then
	RegisterClass{name='MyClass'}
end

